/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'dialog',
    'template'
], function ($, common, ready, dialog, template) {
    var index = function () {
        this.init();
    };
    var payment_windows;
    index.prototype = {
        init: function () {
            this.fetch();
            this.event();

            //浏览器提示
            var isChrome = window.navigator.userAgent.indexOf("Chrome") !== -1;
            var isFirefox = window.navigator.userAgent.indexOf('Firefox') !== -1;
            console.log(isChrome, isFirefox);
            if (!isChrome && !isFirefox) {
                $('#invalid-browser').show();
            }
        },
        fetch: function () {
            var _this = this;
            //获取当天数据
            $.ajax({
                url: _global.url.api + 'order_count',
                type: 'get',
                data: {},
                success: function (res) {
                    if (res.code > 0) {
                        Alert({msg: res.msg, type: 'error'});
                        return;
                    }
                    console.log(res.data.status);
                    if (res.data.is_agreement == 0 && res.data.status == 1) {
                        var data = res.data;
                        var html = template('data-agreement', data);

                        payment_windows = dialog({
                            id: 'edit-data-payment-pay',
                            //title:'商家协议',
                            content: html,
                            width: 900,
                            lock: true,
                        });
                        payment_windows.show();
                    }
                    _this.renderOrder(res.data);
                }
            });
        },

        renderOrder: function (data) {
            $('#new-order').html(data.new);
            $('#refund-order').html(data.refunding);
            $('#today-amount').html(data.today_amount);
            $('#month-amount').html(data.month_amount);
        },
        event: function () {
            $('body').on('click', '.agreement', function () {
                $.ajax({
                    url: _global.url.api + 'is_agreement',
                    type: 'post',
                    data:{ is_agreement:1 },
                    success: function(res) {
                        if(res && res.ret == 0) {
                            Alert({msg: '操作成功'});
                        }else{
                            Alert({msg: '操作失败'});
                        }
                    }
                });
                payment_windows.close();
            });
            $('body').on('click', '[name="is_read"]', function (e) {
                var is_read = $('[name="is_read"]:checked').val();
                if (is_read == 1) {
                    //$('#is_agreement').removeClass('disabled');
                    $("#is_agreement").attr("disabled", false).addClass('btn-primary');
                }else {
                    //$('#is_agreement').addClass('disabled');
                    $("#is_agreement").attr("disabled", true).removeClass('btn-primary');
                }
            });
        }
    };

    new index();

});